package titan.utils;

import java.io.File;

import titan.messages.Messages;

/**
 * Class that allows to read from a text file, replace
 * entries in this text, by informed values, and write the modified text
 */
public class ContentModifier {
	private String originalContent, finalContent;
	private File contentFile;
	private String[] variables;
	private int curVarIndex, curContentIndex;
	private boolean replaceAll;

	public ContentModifier() {
		originalContent = ""; //$NON-NLS-1$
		finalContent = ""; //$NON-NLS-1$
		curVarIndex = 0;
		curContentIndex = 0;
	}

	/**
	 * @see ContentModifier#readContent(String, String[], boolean)
	 */
	public ContentModifier(String local, String[] variables, boolean replaceAll) throws Exception {
		finalContent = ""; //$NON-NLS-1$
		curVarIndex = 0;
		curContentIndex = 0;
		readContent(local, variables, replaceAll);
	}

	/**
	 * Read file content and store variable's names
	 * @param local - the file to be read
	 * @param variables - the variables to be set with {@link ContentModifier#modifyContent modifyContent}
	 * @param replaceAll - if replaceAll is true, {@link ContentModifier#modifyContent modifyContent} will replace all
	 * the variable occurrences, otherwise it will replace just the first one.
	 * @throws Exception if there was an error while reading file
	 */
	public void readContent(String local, String[] variables, boolean replaceAll) throws Exception {
		contentFile = new File(local);
		this.replaceAll = replaceAll;
		//get variables to modify (must be in order if replaceAll is false)
		this.variables = variables;

		originalContent = FileUtils.readTextFile(contentFile);
		if(replaceAll)
			finalContent = originalContent;
	}

	/**
	 * Set the given value to the current variable in the file content. 
	 * If replaceAll is true, it will replace all the variable occurrences,
	 * otherwise it will replace just the first one.
	 * @param value - the value to which variable will be set
	 * @throws Exception if it was unable to find variable in the file content
	 */
	public void modifyContent(String value) throws Exception {
		if(replaceAll)
			finalContent = finalContent.replaceAll(variables[curVarIndex++], value);
		else {
			try{
				int nextContentIndex = originalContent.indexOf(variables[curVarIndex], curContentIndex);
				finalContent += originalContent.substring(curContentIndex, nextContentIndex) + value;
				curContentIndex = nextContentIndex + variables[curVarIndex++].length();
			} catch(Exception e){
				throw new Exception(Messages.UnableFindVar+variables[curVarIndex]+Messages.In+contentFile.getAbsolutePath());
			}
		}
	}

	/**
	 * Get the current file content string
	 * @return the file content
	 */
	public String getContent() {
		if(!replaceAll)
			return finalContent + originalContent.substring(curContentIndex);
		return finalContent;
	}

	/**
	 * Write the current file content string in the source file
	 * @throws Exception if there was an error while writing content to the file
	 */
	public void writeContent() throws Exception {
		if(!replaceAll)
			finalContent += originalContent.substring(curContentIndex);
		FileUtils.writeTextFile(contentFile, finalContent);
	}
}